package android.longs.study.user.dao.user;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import android.longs.study.home.servlet.model.MobleHomeModel;
import android.longs.study.user.model.sub.UserSubjectModel;
import android.longs.study.user.model.user.UserModel;
import android.longs.study.utils.H3Utils;
import android.longs.study.utils.HBUtils;

public class UserDaoImpl implements UserDao {

    @Override
    public UserModel addUser(UserModel userModel) {
	Session currentSession = H3Utils.getCurrentSession();
	currentSession.save(userModel);
	return userModel;
    }

    @Override
    public void updateModel(UserModel userModel) {
	Session currentSession = H3Utils.getCurrentSession();
	currentSession.saveOrUpdate(userModel);

    }

    @Override
    public UserModel queryModelFormId(String id) {
	
	return null;
    }

    @Override
    public UserModel queryModelFormUid(String uid) {
	Session currentSession = H3Utils.getCurrentSession();
	UserModel model = (UserModel) currentSession.createQuery("from UserModel u where u.userUid='"+uid+"'").uniqueResult();
	return model;
    }

    @Override
    public UserModel queryModelFormName(String name) {
	Session currentSession = H3Utils.getCurrentSession();
	List<UserModel> list = currentSession.createQuery("from UserModel u where u.userName='"+name+"'").list();
	if (list==null) {
	    return null;
	}else {
	    if (list.isEmpty()) {
		return null;
	    }else {
		return list.get(0);
	    }
	}

    }

    /*
     * 根据试题的ID 来获取 试题 
     * 
     */
    @Override
    public UserSubjectModel querySubModelFormUid(String uid) {
	Session currentSession = H3Utils.getCurrentSession();
	UserSubjectModel model = (UserSubjectModel) currentSession.createQuery("from UserSubjectModel u where u.uSubUid='"+uid+"'").uniqueResult();
	return model;
    }

    /*
     * 根据试题的ID 和用户的ID  来获取 试题 
     * 
     */
    @Override
    public UserSubjectModel querySubModelFormUid(String subUid, String userUid) {
	Session currentSession = H3Utils.getCurrentSession();
	UserSubjectModel model = (UserSubjectModel) currentSession.createQuery("from UserSubjectModel u where u.uSubUid='"+subUid+"' and u.uSubUserId='"+userUid+"'").uniqueResult();
	return model;
    }

    @Override
    public UserModel queryModelFormNike(String nike) {
	Session currentSession = H3Utils.getCurrentSession();
	UserModel model = (UserModel) currentSession.createQuery("from UserModel u where u.nixe='"+nike+"'").uniqueResult();
	return model;
    }



}
